package com.kawa.kemanage.service.impl;

import com.kawa.kemanage.entity.ContentReview;
import com.kawa.kemanage.entity.News;
import com.kawa.kemanage.entity.SystemNotice;
import com.kawa.kemanage.mapper.ContentReviewMapper;
import com.kawa.kemanage.mapper.NewsMapper;
import com.kawa.kemanage.service.ContentReviewService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kawa.kemanage.service.SystemNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 内容审核服务实现类
 */
@Service
public class ContentReviewServiceImpl implements ContentReviewService {

    @Autowired
    private ContentReviewMapper contentReviewMapper;

    @Autowired
    private NewsMapper newsMapper;

    @Autowired
    private SystemNoticeService systemNoticeService;

    @Override
    @Transactional
    public boolean createReview(ContentReview review) {
        review.setReviewTime(LocalDateTime.now());
        review.setReviewStatus("pending");
        return contentReviewMapper.insertReview(review) > 0;
    }

    @Override
    @Transactional
    public boolean reviewContent(ContentReview contentReview) {
        //设置审核时间
        contentReview.setReviewTime(LocalDateTime.now());

        //更新审核记录
        boolean result = contentReviewMapper.updateReview(contentReview) > 0;

        //构建通知记录
        SystemNotice systemNotice = new SystemNotice();
        systemNotice.setType("system");
        systemNotice.setStatus("unread");
        systemNotice.setCreateTime(new Date());
        systemNotice.setUserId(newsMapper.getNewsById(contentReview.getContentId()));

        if (result && "approved".equals(contentReview.getReviewStatus())) {
            News news = new News();
            news.setId(Math.toIntExact(contentReview.getContentId()));
            news.setIsPublish(1);
            // 如果审核通过，将新闻设置为已发布
            newsMapper.updateNews(news);
            // 给对应的用户发送系统消息
            systemNotice.setTitle("新闻审核通过");
            systemNotice.setContent("新闻审核通过，现已发布！！" + contentReview.getReviewComments());
        }else {
            // 如果审核未通过，给用户发送消息
            systemNotice.setTitle("新闻审核未通过");
            systemNotice.setContent("新闻审核未通过，已被打回，请重新编辑！！" + contentReview.getReviewComments());
        }
        systemNoticeService.addNotice(systemNotice);
        return result;
    }

    @Override
    public PageInfo<ContentReview> getReviewList(String status, Integer categoryId,
                                               String keyword, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<ContentReview> reviews = contentReviewMapper.selectReviewList(status, categoryId, keyword, null, null);
        return new PageInfo<>(reviews);
    }

    @Override
    public ContentReview getReviewByContentId(Long contentId) {
        return contentReviewMapper.selectReviewByContentId(contentId);
    }

    @Override
    public ContentReview getReviewById(Long reviewId) {
        return contentReviewMapper.selectReviewById(reviewId);
    }

    @Override
    @Transactional
    public boolean batchReview(List<Long> reviewIds, String status, String comments, Long reviewerId) {
        int count = 0;
        for (Long reviewId : reviewIds) {
            ContentReview contentReview = new ContentReview();
            contentReview.setReviewId(reviewId);
            contentReview.setReviewStatus(status);
            contentReview.setReviewComments(comments);
            contentReview.setReviewerId(reviewerId);
            if (reviewContent(contentReview)) {
                count++;
            }
        }
        return count == reviewIds.size();
    }
}
